package com.example.ccj.b.c.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;

import com.example.ccj.R;
import com.example.ccj.b.c.custom.MImageView;
import com.example.ccj.b.c.custom.MTextView;
import com.example.ccj.b.c.draw.TopBar;
import com.example.ccj.u.ToastUtils;


/**
 * Dialog模板
 */
public abstract class DialogBase extends Dialog {
    private static final String TAG = "DialogBase";

    public DialogBase(@NonNull Context context) {
        super(context);
        initBaseView();
    }

    //显示位置
    private int gravity = Gravity.CENTER;
    //是否占满宽度
    private boolean isFull = false;
    //继承类布局
    private @LayoutRes int inheritLayout = 0;
    //头部容器
    private LinearLayout mLlTitle;
    //中部自定义容器
    private LinearLayout mLlBoot;
    //中部容器
    private LinearLayout mLlText;
    //底部按钮
    private LinearLayout mLlBottom;
    //头部左侧icon
    private MImageView mTitleLeftIcon;
    //头部右侧icon
    private MImageView mTitleRightIcon;
    //头部中间文字
    private TextView mTvTitleText;
    //中间中间文字
    private TextView mTvCenterText;
    //确认按钮
    private MTextView mTvButtonSure;
    //高度按比例显示
    private boolean isShowHeightRatio = false;
    //高度显示比例
    private double heightRatio = 1;
    //取消按钮
    private MTextView mTvButtonCancel;
    //继续按钮
    private MTextView mTvButtonContinue;
    //透明度
    private float alpha = 0.8f;
    //是否满屏宽度显示
    private boolean installWidth = true;
    //点击空白位置是否可退出
    private boolean isCanClose = true;
    //屏幕背景虚化透明度
    private float dimAmount = 0.5f;
    //显示一半的高度
    boolean heightHalf = false;

    TopBar topBar;

    private DialogStopListener stopListener;
    private DialogButtonOnClickListener clickListener;
    @Override
    protected void onStop() {
        super.onStop();
        if (stopListener != null) stopListener.onStop();
    }

    public DialogButtonOnClickListener getClickListener() {
        return clickListener;
    }

    public void setClickListener(DialogButtonOnClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public interface DialogStopListener{
        void onStop();
    }

    public DialogStopListener getStopListener() {
        return stopListener;
    }

    public void setStopListener(DialogStopListener stopListener) {
        this.stopListener = stopListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void onStart() {
        super.onStart();
        onInit();
        baseInit();
    }

    private void baseInit(){
        //是否全屏
        if (isFull) {
            //沾满宽度设置
            fullInstall();
        } else {
            //不沾满宽度设置
            noFullInstall();
        }
    }

    private void initBaseView(){
        setContentView(R.layout.dialog_base);
        mLlTitle = findViewById(R.id.dialog_base_title);
        mLlText = findViewById(R.id.dialog_base_ll_text);
        mLlBoot = findViewById(R.id.dialog_base_boot);
        mLlBottom = findViewById(R.id.dialog_base_bottom_type);

//        mTitleLeftIcon = findViewById(R.id.dialog_base_left_icon);
        mTitleRightIcon = findViewById(R.id.dialog_base_right_icon);
//        mTvTitleText = findViewById(R.id.dialog_base_title_name);
        mTvCenterText = findViewById(R.id.dialog_base_center_text);
        mTvButtonSure = findViewById(R.id.dialog_base_sure);
        mTvButtonCancel = findViewById(R.id.dialog_base_cancel);
        mTvButtonContinue = findViewById(R.id.dialog_base_cancel1);
        topBar = findViewById(R.id.base_dialog_top);


        mTitleRightIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

    }


    public abstract void onInit();

    //-----------------------------以下是底部按钮部设置------------------------------------
    /**
     * 确定按钮文本，并设置了默认的点击事件
     */
    public TextView setButtonSureText(String value){
        mLlBottom.setVisibility(View.VISIBLE);
        mTvButtonSure.setVisibility(View.VISIBLE);
        if (!value.isEmpty()) {
            mTvButtonSure.setText(value);
        }
        return mTvButtonSure;
    }

    /**
     * 设置确定按钮文本及 默认的点击事件
     * 具体实现该 事件需要设置 clickListener 属性
     * @param value
     */
    public void setButtonSureTextAndDefaultClickListener(String value){
        mLlBottom.setVisibility(View.VISIBLE);
        mTvButtonSure.setVisibility(View.VISIBLE);
        if (!value.isEmpty()) {
            mTvButtonSure.setText(value);
        }
        mTvButtonSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickListener != null) clickListener.dialogSure();
                dismiss();
            }
        });
    }

    /**
     * 确定按钮文本，并设置了默认的点击事件
     */
    public TextView setButtonSureClick(View.OnClickListener clickListener){
        if (clickListener != null) {
            mTvButtonSure.setOnClickListener(clickListener);
        }
        return mTvButtonSure;
    }

    /**
     * 取消按钮文本并设置了默认的点击事件
     */
    public TextView setButtonCancelText(String value){
        mLlBottom.setVisibility(View.VISIBLE);
        mTvButtonCancel.setVisibility(View.VISIBLE);
        if (!value.isEmpty()) {
            mTvButtonCancel.setText(value);
        }
        return mTvButtonCancel;
    }

    /**
     * 设置取消按钮文本及 默认的点击事件
     * 具体实现该 事件需要设置 clickListener 属性
     * @param value
     */
    public void setButtonCancelTextAndDefaultClickListener(String value){
        mLlBottom.setVisibility(View.VISIBLE);
        mTvButtonCancel.setVisibility(View.VISIBLE);
        if (!value.isEmpty()) {
            mTvButtonCancel.setText(value);
        }
        mTvButtonCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickListener != null) clickListener.dialogCancel();
                dismiss();
            }
        });
    }

    /**
     * 确定按钮文本，并设置了默认的点击事件
     */
    public TextView setButtonCancelClick(View.OnClickListener clickListener){
        if (clickListener != null) {
            mTvButtonCancel.setOnClickListener(clickListener);
        }
        return mTvButtonCancel;
    }

    /**
     * 继续按钮文本
     */
    public TextView setButtonContinueText(String value){
        mLlBottom.setVisibility(View.VISIBLE);
        mTvButtonContinue.setVisibility(View.VISIBLE);
        if (!value.isEmpty()) {
            mTvButtonContinue.setText(value);
        }
        return mTvButtonContinue;
    }

    /**
     * 设置继续按钮文本及 默认的点击事件
     * 具体实现该 事件需要设置 clickListener 属性
     * @param value
     */
    public void setButtonContinueTextAndDefaultClickListener(String value){
        mLlBottom.setVisibility(View.VISIBLE);
        mTvButtonContinue.setVisibility(View.VISIBLE);
        if (!value.isEmpty()) {
            mTvButtonContinue.setText(value);
        }
        mTvButtonContinue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clickListener != null) clickListener.dialogContinue();
                dismiss();
            }
        });
    }

    /**
     * 确定按钮文本，并设置了默认的点击事件
     */
    public TextView setButtonContinueClick(View.OnClickListener clickListener){
        if (clickListener != null) {
            mTvButtonContinue.setOnClickListener(clickListener);
        }
        return mTvButtonContinue;
    }
    //-----------------------------以上是底部按钮部设置------------------------------------



//-----------------------------以下是中部文本设置--------------------------
    /**
     * 添加中间固定文本
     */
    public TextView setCenterText(String value){
        mLlText.setVisibility(View.VISIBLE);
        if (!value.isEmpty()) {
            mTvCenterText.setText(value);
        }
        return mTvCenterText;
    }
    //-----------------------------以上是中部文本设置------------------------------------



    //-----------------------------以下是头部设置--------------------------
    /**
     * 头部左侧图标
     */
    public ImageView setTitleLeftIcon(int icon){
        mLlTitle.setVisibility(View.VISIBLE);
        if (icon != 0) {
            topBar.setLeftIco(icon);
//            mTitleLeftIcon.setImageResource(icon);
        }
        return topBar.getTopLeftImage();
    }

    /**
     * 头部左侧图片点击事件
     */
    public ImageView setTitleLeftIcon(View.OnClickListener clickListener){
        if (clickListener != null) {
            topBar.setLeftClick(clickListener);
//            mTitleLeftIcon.setOnClickListener(clickListener);
        }
        return topBar.getTopRightImage();
    }

    /**
     * 头部右侧图标
     */
    public ImageView setTitleRightIcon(int icon){
        mLlTitle.setVisibility(View.VISIBLE);
        if (icon != 0) {
            topBar.setRightIcon(icon);
//            mTitleRightIcon.setImageResource(icon);
        }
        return topBar.getTopRightImage();
    }

    /**
     * 头部右侧图片点击事件
     */
    public ImageView setTitleRightIcon(View.OnClickListener clickListener){
        mLlTitle.setVisibility(View.VISIBLE);
        if (clickListener != null) {
            topBar.setRightClick(clickListener);
//            mTitleRightIcon.setOnClickListener(clickListener);
        }
        return topBar.getTopRightImage();
    }

    /**
     * 添加头部中间固定文本
     */
    public TextView setTitleCenterText(String value){
        topBar.getTopTitle().setGravity(Gravity.CENTER);
        mLlTitle.setVisibility(View.VISIBLE);
        if (!value.isEmpty()) {
            topBar.setTitle(value);
        }
        return topBar.getTopTitle();
    }

    public TextView setTitleLeftText(String value){
        mLlTitle.setVisibility(View.VISIBLE);
        topBar.setLeftText(value);
        return topBar.getLeftText();
    }

    public TextView setTitleRightText(String value){
        mLlTitle.setVisibility(View.VISIBLE);
        topBar.setRightText(value);
        return topBar.getRightText();
    }

    public void goneTitleLeftImage(){
        if (topBar.getVisibility() == View.VISIBLE) {
            topBar.setLeftImageHidden();
        }
    }

    public void goneTitleRightImage(){
        if (topBar.getVisibility() == View.VISIBLE) {
            topBar.setRightHidden();
        }
    }


//-----------------------------以上是头部设置------------------------------------


//-----------------------------以下是显示位置设置------------------------------------
    /**
     * 显示位置 例：Gravity.CENTER
     * @param gravity 例：Gravity.CENTER
     */
    protected void setBaseGravity(int gravity) {
        this.gravity = gravity;
    }
    /**
     * 显示位置在中间
     */
    protected void showInCenter() {
        this.gravity = Gravity.CENTER;
    }
    /**
     * 显示位置在底部
     */
    protected void showInBottom() {
        this.gravity = Gravity.BOTTOM;
    }
//-----------------------------以上是显示位置设置------------------------------------
    /**
     * 是否全屏
     * @param isFull 宽度是否占满屏幕
     */
    protected void setIsFull(boolean isFull){
        this.isFull = isFull;
    }

    /**
     * 显示底部按钮
     * 要设置每个按钮的文本才能真正显示
     */
    public void showBottomView(){
        mLlBottom.setVisibility(View.VISIBLE);
    }

    //关闭点击事件
    private View.OnClickListener defaultCloseOnClick(){
        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        };
    }
    //提示点击事件
    private View.OnClickListener defaultOnClick(){
        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtils.showShort("未设置！");
            }
        };
    }


    protected void noInstallWidth(){
        installWidth = false;
    }


    /**
     * 不是占满宽度的设置
     */
    private void noFullInstall(){
        //设置宽度
        WindowManager m = getWindow().getWindowManager();
        Display d = m.getDefaultDisplay();
        WindowManager.LayoutParams p = getWindow().getAttributes();
        Point size = new Point();
        if (installWidth) {
            d.getRealSize(size);
            p.width = (int)(size.x);
            p.x = 0;
            p.y = 0;
        }
        p.dimAmount = dimAmount;
        p.alpha = alpha;
        p.gravity = gravity;
        getWindow().setAttributes(p);
        setCancelable(isCanClose);

    }

    boolean isShowMAx = false;

    protected void isShowHeightMax(boolean isShowMAx){
        this.isShowMAx = isShowMAx;
    }

    protected void isShowBackground(boolean isShow){
        if (isShow) {
            getWindow().setBackgroundDrawableResource(R.color.text_white_color);
        } else {
            getWindow().setBackgroundDrawableResource(R.color.colpr_dialog_derermine_background);

        }
    }

    public void isCanClose(boolean canClose){
        isCanClose = canClose;
        mTitleRightIcon.setVisibility(View.GONE);
    }


    public void setHeight(){
        heightHalf = true;
    }



    /**
     * 占满宽度的设置
     */
    private void fullInstall(){
        //设置布局
        Window window = getWindow();
        // 把 DecorView 的默认 padding 取消，同时 DecorView 的默认大小也会取消
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        // 设置宽度
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        // 设置高度
        Display display = getWindow().getWindowManager().getDefaultDisplay();
        if (isShowMAx) {
            layoutParams.height = display.getHeight();
        } else if (heightHalf) {
            layoutParams.height = display.getHeight()/3+display.getHeight()/2;
        } else if (isShowHeightRatio) {
            double v = display.getHeight() * heightRatio;
            layoutParams.height = (int) v;
        }

        //位置
        layoutParams.gravity = gravity;
        layoutParams.alpha = alpha;
//        layoutParams.dimAmount = 0;
        setCancelable(isCanClose);
        window.setAttributes(layoutParams);
        // 给 DecorView 设置背景颜色，很重要，不然导致 Dialog 内容显示不全，有一部分内容会充当 padding，上面例子有举出
        window.getDecorView().setBackgroundColor(Color.WHITE);
    }

    public float getDimAmount() {
        return dimAmount;
    }

    protected void setDimAmount(float dimAmount) {
        this.dimAmount = dimAmount;
    }

    public int getGravity() {
        return gravity;
    }

    protected void setGravity(int gravity) {
        this.gravity = gravity;
    }

    public boolean isFull() {
        return isFull;
    }

    protected void setFull(boolean full) {
        isFull = full;
    }

    public int getInheritLayout() {
        return inheritLayout;
    }

    protected void setLayout(@LayoutRes int inheritLayout) {
        mLlBoot.setVisibility(View.VISIBLE);
        //            LayoutInflater.from(getContext()).inflate(inheritLayout,null);
//        mLlBoot.addView(getLayoutInflater().inflate(inheritLayout,null));
        mLlBoot.removeAllViews();
        mLlBoot.addView(getLayoutInflater().inflate(inheritLayout,null));
        this.inheritLayout = inheritLayout;
    }

    public LinearLayout getmLlTitle() {
        return mLlTitle;
    }

    protected void setmLlTitle(LinearLayout mLlTitle) {
        this.mLlTitle = mLlTitle;
    }

    public LinearLayout getmLlBoot() {
        return mLlBoot;
    }

    protected void setmLlBoot(LinearLayout mLlBoot) {
        this.mLlBoot = mLlBoot;
    }

    public LinearLayout getmLlText() {
        return mLlText;
    }

    protected void setmLlText(LinearLayout mLlText) {
        this.mLlText = mLlText;
    }

    public LinearLayout getmLlBottom() {
        return mLlBottom;
    }

    protected void setmLlBottom(LinearLayout mLlBottom) {
        this.mLlBottom = mLlBottom;
    }

    public MImageView getmTitleLeftIcon() {
        return mTitleLeftIcon;
    }

    protected void setmTitleLeftIcon(MImageView mTitleLeftIcon) {
        this.mTitleLeftIcon = mTitleLeftIcon;
    }

    public MImageView getmTitleRightIcon() {
        return mTitleRightIcon;
    }

    protected void setmTitleRightIcon(MImageView mTitleRightIcon) {
        this.mTitleRightIcon = mTitleRightIcon;
    }

    public TextView getmTvTitleText() {
        return mTvTitleText;
    }

    protected void setmTvTitleText(TextView mTvTitleText) {
        this.mTvTitleText = mTvTitleText;
    }

    public TextView getmTvCenterText() {
        return mTvCenterText;
    }

    protected void setmTvCenterText(TextView mTvCenterText) {
        this.mTvCenterText = mTvCenterText;
    }

    public MTextView getmTvButtonSure() {
        return mTvButtonSure;
    }

    protected void setmTvButtonSure(MTextView mTvButtonSure) {
        this.mTvButtonSure = mTvButtonSure;
    }

    public MTextView getmTvButtonCancel() {
        return mTvButtonCancel;
    }

    protected void setmTvButtonCancel(MTextView mTvButtonCancel) {
        this.mTvButtonCancel = mTvButtonCancel;
    }

    public MTextView getmTvButtonContinue() {
        return mTvButtonContinue;
    }

    protected void setmTvButtonContinue(MTextView mTvButtonContinue) {
        this.mTvButtonContinue = mTvButtonContinue;
    }

    public void setAlpha(float alpha) {
        this.alpha = alpha;
    }

    public boolean isShowHeightRatio() {
        return isShowHeightRatio;
    }

    public void setShowHeightRatio(boolean showHeightRatio) {
        isShowHeightRatio = showHeightRatio;
    }

    public double getHeightRatio() {
        return heightRatio;
    }

    public void setHeightRatio(double heightRatio) {
        this.heightRatio = heightRatio;
    }
}
